package com.caoustc.okhttplib.utils

object ArrayUtils {
    fun <V> isEmpty(sourceArray: Array<V>?): Boolean {
        return sourceArray == null || sourceArray.size == 0
    }

    fun <V> getLast(sourceArray: Array<V>?, value: V, defaultValue: V?, isCircle: Boolean): V? {
        if (isEmpty(sourceArray)) {
            return defaultValue
        }
        var currentPosition = -1
        for (i in sourceArray!!.indices) {
            if (ObjectUtils.isEquals(value, sourceArray[i])) {
                currentPosition = i
                break
            }
        }
        if (currentPosition == -1) {
            return defaultValue
        }
        return if (currentPosition == 0) {
            if (isCircle) sourceArray[sourceArray.size - 1] else defaultValue
        } else sourceArray[currentPosition - 1]
    }

    fun <V> getNext(sourceArray: Array<V>?, value: V, defaultValue: V?, isCircle: Boolean): V? {
        if (isEmpty(sourceArray)) {
            return defaultValue
        }
        var currentPosition = -1
        for (i in sourceArray!!.indices) {
            if (ObjectUtils.isEquals(value, sourceArray[i])) {
                currentPosition = i
                break
            }
        }
        if (currentPosition == -1) {
            return defaultValue
        }
        return if (currentPosition == sourceArray.size - 1) {
            if (isCircle) sourceArray[0] else defaultValue
        } else sourceArray[currentPosition + 1]
    }

    fun <V> getLast(sourceArray: Array<V>?, value: V, isCircle: Boolean): V? {
        return getLast(sourceArray, value, null, isCircle)
    }

    fun <V> getNext(sourceArray: Array<V>?, value: V, isCircle: Boolean): V? {
        return getNext(sourceArray, value, null, isCircle)
    }

    fun getLast(sourceArray: LongArray, value: Long, defaultValue: Long, isCircle: Boolean): Long? {
        require(sourceArray.size != 0) { "The length of source array must be greater than 0." }
        val array = ObjectUtils.transformLongArray(sourceArray)
        return getLast(array, value, defaultValue, isCircle)
    }

    fun getNext(sourceArray: LongArray, value: Long, defaultValue: Long, isCircle: Boolean): Long? {
        require(sourceArray.size != 0) { "The length of source array must be greater than 0." }
        val array = ObjectUtils.transformLongArray(sourceArray)
        return getNext(array, value, defaultValue, isCircle)
    }

    fun getLast(sourceArray: IntArray, value: Int, defaultValue: Int, isCircle: Boolean): Int? {
        require(sourceArray.size != 0) { "The length of source array must be greater than 0." }
        val array = ObjectUtils.transformIntArray(sourceArray)
        return getLast(array, value, defaultValue, isCircle)
    }

    fun getNext(sourceArray: IntArray, value: Int, defaultValue: Int, isCircle: Boolean): Int? {
        require(sourceArray.size != 0) { "The length of source array must be greater than 0." }
        val array = ObjectUtils.transformIntArray(sourceArray)
        return getNext(array, value, defaultValue, isCircle)
    }
}